﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.Setting
{
    public partial class ucClassify : UserControl
    {
        #region UserControl

        public ucClassify()
        {
            InitializeComponent();
            loadData();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = Main.AeroColor;
        }

        #endregion

        #region Event

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.lblStatus.Text = this.btnAdd.Text;
            this.tbClassify.Text = "";
            this.pRight.Tag = "";
            this.pRight.Visible = true;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvClassify.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择需要修改的记录");
                return;
            }
            if (this.dgvClassify.SelectedRows.Count > 1)
            {
                MessageBox.Show("每次只能修改一条记录");
                return;
            }

            this.lblStatus.Text = this.btnModify.Text;
            if (this.dgvClassify.SelectedRows[0].Cells[3].Value.ToString() == "0") this.rbtnInCome.Checked = true;
            else this.rbtnExpense.Checked = true;
            this.tbClassify.Text = this.dgvClassify.SelectedRows[0].Cells[0].Value.ToString();
            this.pRight.Tag = this.dgvClassify.SelectedRows[0].Cells[1].Value.ToString();
            this.pRight.Visible = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvClassify.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的记录");
                return;
            }

            List<string> guids = new List<string>();
            foreach (DataGridViewRow row in this.dgvClassify.SelectedRows) guids.Add(row.Cells[1].Value.ToString());

            if (MoneyClass.Classify.Delete(guids) < 1)
            {
                MessageBox.Show("操作失败");
                return;
            }

            loadData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.rbtnInCome.Checked && !this.rbtnExpense.Checked)
            {
                MessageBox.Show("内部错误");
                return;
            }
            if (string.IsNullOrEmpty(this.tbClassify.Text))
            {
                MessageBox.Show("分类名称不能为空");
                return;
            }

            int rval = 0;
            int type = -1;
            if (this.rbtnInCome.Checked) type = 0;
            if (this.rbtnExpense.Checked) type = 1;
            if (this.lblStatus.Text == this.btnAdd.Text) rval = MoneyClass.Classify.Add(this.tbClassify.Text, type, Main.CurrentGuid);
            else rval = MoneyClass.Classify.Update(this.pRight.Tag.ToString(), this.tbClassify.Text, type, Main.CurrentGuid);

            switch (rval)
            {
                case -2:
                    MessageBox.Show("分类名称已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    break;
                case 1:
                    MessageBox.Show("操作成功");
                    loadData();
                    this.pRight.Visible = false;
                    break;
                default:
                    MessageBox.Show("内部错误，代码：" + rval);
                    break;
            }
        }

        private void dgvClassify_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}条记录", this.dgvClassify.RowCount);
        }

        private void pbHide_Click(object sender, EventArgs e)
        {
            this.pRight.Visible = false;
        }

        #endregion

        #region Method

        private void loadData()
        {
            DataTable dt = MoneyClass.Classify.get(Main.CurrentGuid);
            this.dgvClassify.AutoGenerateColumns = false;
            this.dgvClassify.Columns[0].DataPropertyName = "classify";
            this.dgvClassify.Columns[1].DataPropertyName = "guid";
            this.dgvClassify.Columns[2].DataPropertyName = "typeText";
            this.dgvClassify.Columns[3].DataPropertyName = "type";
            this.dgvClassify.DataSource = dt;
        }

        #endregion
    }
}
